﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WuDada.Core.Post.Domain;
using WuDada.Core.Persistence.Ado;
using WuDada.Core.Persistence;
using NHibernate.Criterion;
using System.Collections;
using WuDada.Core.Generic.Extension;

namespace WuDada.Core.Post.Persistence
{
    public class AnswerDao : AdoDao, IAnswerDao
    {
        public INHibernateDao NHibernateDao { get; set; }        

        /// <summary>
        /// 新增Answer
        /// </summary>
        /// <param name="answerVO">被新增的Answer</param>
        /// <returns>新增後的Answer</returns>
        public AnswerVO CreateAnswer(AnswerVO answerVO)
        {
            NHibernateDao.Insert(answerVO);

            return answerVO;
        }

        /// <summary>
        /// 取得Answer By AnswerId
        /// </summary>
        /// <param name="answerId">AnswerId</param>
        /// <returns>Answer</returns>
        public AnswerVO GetAnswerById(int answerId)
        {
            return NHibernateDao.GetVOById<AnswerVO>(answerId);
        }

        /// <summary>
        /// 刪除Answer
        /// </summary>
        /// <param name="answerVO">被刪除的Answer</param>
        public void DeleteAnswer(AnswerVO answerVO)
        {
            NHibernateDao.Delete(answerVO);
        }

        /// <summary>
        /// 更新Answer
        /// </summary>
        /// <param name="answerVO">被更新的Answer</param>
        /// <returns>更新後的Answer</returns>
        public AnswerVO UpdateAnswer(AnswerVO answerVO)
        {
            NHibernateDao.Update(answerVO);

            return answerVO;
        }

        /// <summary>
        /// 取得Answer清單
        /// </summary>
        /// <param name="conditions">搜尋條件</param>
        /// <returns>Answer清單</returns>
        public IList<AnswerVO> GetAnswerList(IDictionary<string, string> conditions)
        {
            ArrayList param = new ArrayList();
            string fromScript = "select a from AnswerVO a ";
            StringBuilder whereScript = new StringBuilder();
            return this.QueryAnswer(param, fromScript, whereScript, conditions, true).OfType<AnswerVO>().ToList();
        }

        /// <summary>
        /// 取得Answer數量
        /// </summary>
        /// <param name="conditions"></param>
        /// <returns></returns>
        public int GetAnswerCount(IDictionary<string, string> conditions)
        {
            int count = 0;
            ArrayList param = new ArrayList();
            string fromScript = "select count(a.AnswerId) from AnswerVO a ";
            StringBuilder whereScript = new StringBuilder();
            IList result = this.QueryAnswer(param, fromScript, whereScript, conditions, false);
            if (result.Count > 0)
            {
                count = Convert.ToInt32(result[0]);
            }
            return count;
        }

        private IList QueryAnswer(ArrayList param, string fromScript, StringBuilder whereScript, IDictionary<string, string> conditions, bool useOrder)
        {
            AppendAnswerKeyWord(conditions, whereScript, param);
            AppendAnswerPost(conditions, whereScript, param);
            AppendAnswerMember(conditions, whereScript, param);
            AppendAnswerMemberKeyWord(conditions, whereScript, param);
            AppendAnswerFlag(conditions, whereScript, param);
            AppendAnswerApplyForVerification(conditions, whereScript, param);
            AppendAnswerApplyForVerificationStatus(conditions, whereScript, param);
            AppendAnswerResult(conditions, whereScript, param);

            string hql = fromScript + "where 1=1 " + whereScript;
            if (useOrder)
            {
                hql += AppendAnswerOrder(conditions);
            }

            return NHibernateDao.Query(hql, param, conditions);
        }

        private void AppendAnswerMemberKeyWord(IDictionary<string, string> conditions, StringBuilder whereScript, ArrayList param)
        {
            if (conditions.IsContainsValue("MemberKeyWord"))
            {
                whereScript.Append(" and (a.Member.Name like ? or a.Member.Email like ? or a.Member.Phone like ? or a.Member.Mobile like ? ) ");
                param.Add("%" + conditions["MemberKeyWord"] + "%");
                param.Add("%" + conditions["MemberKeyWord"] + "%");
                param.Add("%" + conditions["MemberKeyWord"] + "%");
                param.Add("%" + conditions["MemberKeyWord"] + "%");
            }
        }

        private void AppendAnswerApplyForVerification(IDictionary<string, string> conditions, StringBuilder whereScript, ArrayList param)
        {
            if (conditions.IsContainsValue("ApplyForVerification"))
            {
                whereScript.Append(" and a.ApplyForVerification = ? ");
                param.Add(bool.Parse(conditions["ApplyForVerification"]));
            }
        }

        private void AppendAnswerApplyForVerificationStatus(IDictionary<string, string> conditions, StringBuilder whereScript, ArrayList param)
        {
            if (conditions.IsContainsValue("ApplyForVerificationStatus"))
            {
                whereScript.Append(" and a.ApplyForVerificationStatus = ? ");
                param.Add(conditions["ApplyForVerificationStatus"]);
            }
        }

        private void AppendAnswerResult(IDictionary<string, string> conditions, StringBuilder whereScript, ArrayList param)
        {
            if (conditions.IsContainsValue("Result"))
            {
                whereScript.Append(" and a.Result = ? ");
                param.Add(conditions["Result"]);
            }
        }

        private void AppendAnswerMember(IDictionary<string, string> conditions, StringBuilder whereScript, ArrayList param)
        {
            if (conditions.IsContainsValue("MemberId"))
            {
                whereScript.Append(" and a.Member.MemberId = ? ");
                param.Add(conditions["MemberId"]);
            }
        }

        private void AppendAnswerPost(IDictionary<string, string> conditions, StringBuilder whereScript, ArrayList param)
        {
            if (conditions.IsContainsValue("PostId"))
            {
                whereScript.Append(" and a.PostId = ? ");
                param.Add(conditions["PostId"]);
            }
        }

        private void AppendAnswerFlag(IDictionary<string, string> conditions, StringBuilder whereScript, ArrayList param)
        {
            if (conditions.IsContainsValue("Flag"))
            {
                whereScript.Append(" and a.Flag = ? ");
                param.Add(conditions["Flag"]);
            }
        }

        private void AppendAnswerKeyWord(IDictionary<string, string> conditions, StringBuilder whereScript, ArrayList param)
        {
            if (conditions.IsContainsValue("KeyWord"))
            {
                whereScript.Append(" and (a.PostTitle like ? ) ");
                param.Add("%" + conditions["KeyWord"] + "%");
            }
        }

        private string AppendAnswerOrder(IDictionary<string, string> conditions)
        {
            //// 排序條件
            string order = "order by a.SortNo, a.CreatedDate desc ";
            if (conditions.IsContainsValue("Order"))
            {
                order = conditions["Order"];
            }

            return order;
        }
    }
}
